﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;

#if WINDOWS_PHONE
using Microsoft.Phone.Shell;
#endif

namespace BattleCity
{
    public static class Options
    {
        public static Microsoft.Xna.Framework.GraphicsDeviceManager graphicsManager;

        public static bool enhancedBullets = false;
        public static bool tanksLights = false;
        public static bool antialiasingEnabled { get { return _antialiasing; } }

        public static CurrentGameState tomboneState = CurrentGameState.Menu;

        private static  bool _antialiasing;

        public static void setAntialiasing(bool activated)
        {
            _antialiasing = activated;
            if (graphicsManager != null)
            {
                graphicsManager.PreferMultiSampling = activated;
                graphicsManager.ApplyChanges();
            }

        }

        public static bool soundEnabled = true;

        public static SerializableOptions generateSerializableOptions()
        {
            SerializableOptions options = new SerializableOptions();
            options.sound = Options.soundEnabled;
            options.antialiasing = Options.antialiasingEnabled;
            options.enBullets = Options.enhancedBullets;
            options.tankLights = Options.tanksLights;
            options.gameState = Options.tomboneState;

            return options;
        }

        public static void saveOptions()
        {
            #if WINDOWS_PHONE
            IsolatedStorageFile IS = IsolatedStorageFile.GetUserStoreForApplication();
            #else
            IsolatedStorageFile IS = IsolatedStorageFile.GetUserStoreForDomain();
            #endif
            try
            {
                if (IS.FileExists("options.sav")) IS.DeleteFile("options.sav");

                using (var saveFile = IS.CreateFile("options.sav"))
                {
                    var serializer = new XmlSerializer(typeof(SerializableOptions));
                    serializer.Serialize(saveFile, generateSerializableOptions());
                    saveFile.Close();
                }
            }
            catch (Exception e)
            {
                System.Console.Out.Write(e.Message);
            }
        }

        public static void loadOptions()
        {
            #if WINDOWS_PHONE
            IsolatedStorageFile IS = IsolatedStorageFile.GetUserStoreForApplication();
            #else
            IsolatedStorageFile IS = IsolatedStorageFile.GetUserStoreForDomain();
            #endif

            if (IS.FileExists("options.sav"))
            {
                try
                {
                    SerializableOptions gameOptions;
                    using (var saveFile = IS.OpenFile("options.sav", System.IO.FileMode.Open))
                    {
                        var deserializer = new XmlSerializer(typeof(SerializableOptions));
                        gameOptions = (SerializableOptions)deserializer.Deserialize(saveFile);
                        saveFile.Close();
                    }

#if WINDOWS_PHONE
                    if(PhoneApplicationService.Current.StartupMode == StartupMode.Activate)
                    {
                        Options.tomboneState = gameOptions.gameState;
                        // game is resuming from tombstoning. Restore any transient data that was saved.
                    }
#endif


                    Options.soundEnabled = gameOptions.sound;
                    Options.enhancedBullets = gameOptions.enBullets;
                    Options.setAntialiasing(gameOptions.antialiasing);
                    Options.tanksLights = gameOptions.tankLights;

                }
                catch (Exception e)
                {
                    if (IS.FileExists("options.sav"))
                    {
                        IS.DeleteFile("options.sav");
                    }
                }
            }
        }
    }

    public struct SerializableOptions
    {
        public bool sound;
        public bool antialiasing;
        public bool enBullets;
        public bool tankLights;
        public CurrentGameState gameState;
    }

    public enum CurrentGameState
    {
        Menu,
        SingleLevel,
        Survival
    }
}
